package com.mission.game.playgame;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mission.game.R;
import com.mission.game.components.StatusAdapter;
import com.mission.game.otherscreens.About;
import com.mission.game.utils.Game;
import com.mission.game.utils.Mission;

public class PlayGameScreen  extends Activity {
	private Game _game;
//	private ArrayList<Mission> _missions;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play_game_screen);

		Bundle extras = getIntent().getExtras();
		_game = (Game) extras.getSerializable("game");

		setFields();
		
		// Set chosen missions list
		setCurrentMissions();
	}
	private void setFields() {
		((TextView) findViewById(R.id.game_name)).setText(_game.getName());
		((TextView) findViewById(R.id.game_description)).setText(_game.getDescription());
	}


	private void setCurrentMissions() {
		final ArrayList<Mission> missions = _game.getMissionList();
		ListView missionsLv = (ListView) findViewById(R.id.chosen_missions_list);
		StatusAdapter<Mission> missionAdapter = new StatusAdapter<Mission>(this,
				R.layout.status_list_item, missions) ;
		missionsLv.setAdapter(missionAdapter);
		missionsLv.setTextFilterEnabled(true);
		missionsLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(getApplicationContext(),
						PlayMissionScreen.class);
				i.putExtra("mission", missions.get(position));
				startActivityForResult(i, 0);
			}

		});
		
		((TextView) findViewById(R.id.score)).setText("Score: "+_game.getScore());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && data != null
				&& data.getExtras() != null) {
			Mission mission = (Mission) data.getExtras().get("mission");
			_game.updateMission(mission);
			setCurrentMissions();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		setCurrentMissions();
	}
	
	@Override
	public void onBackPressed() {
		getIntent().putExtra("game", _game);
		setResult(RESULT_OK, getIntent());
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.game, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, About.class));
			return true;
		case R.id.removeMission:
			getIntent().putExtra("game", _game);
			setResult(RESULT_FIRST_USER, getIntent());
			finish();
			return true;
		}
		return false;
	}
}
